<?php

namespace app\common\dao\system;

use app\common\model\system\SystemConfigClassify as model;
use app\common\dao\BaseDao;

class SystemConfigClassifyDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }

    public function getOptions()
    {
        return ($this->getModel())::column('pid,classify_name', 'config_classify_id');
    }

    public function getTopOptions()
    {
        return ($this->getModel())::getDB()->where('pid', 0)->column('classify_name', 'config_classify_id');
    }

    public function all()
    {
        return ($this->getModel())::getDB()->select();
    }

    public function count()
    {
        return ($this->getModel())::getDB()->count();
    }

    public function children(int $pid, string $field = 'id,classify_name')
    {
        return ($this->getModel())::getDB()->where('pid', $pid)->field($field)->select();
    }

    public function existsChild(int $id): bool
    {
        return $this->fieldExists('pid', $id);
    }

    public function keyExists($key, ?int $except = null): bool
    {
        return $this->fieldExists('classify_key', $key, $except);
    }


    public function pidExists(int $pid, ?int $except = null): bool
    {
        return $this->fieldExists('config_classify_id', $pid, $except);
    }


    public function keyById(string $key)
    {
        return ($this->getModel())::getDB()->where('classify_key', $key)->value('config_classify_id');
    }


    public function keyByData(string $key)
    {
        return ($this->getModel())::getDB()->where('classify_key', $key)->find();
    }


}